package com.soilflow.web.utils;

import com.soilflow.common.exception.ServiceException;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author steven
 * @date 2022年08月19日 15:21:15
 */
public class DownLoadUtils {

    public static final String URL = "https://cdn.weikedashi.net/tSsYXhGM1660963073255?attname=soilflow_education_database.docx";

    public static final String introduceUrl = "";

    /**
     * 根据url
     * @param Url
     */
    public static InputStream downLoadByUrl(String Url){
        URL url = null;
        InputStream inputStream = null;
        try {
            url = new URL(Url);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            //设置超时间为3秒
            conn.setConnectTimeout(3*1000);
            //防止屏蔽程序抓取而返回403错误
            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
            //得到输入流
            inputStream = conn.getInputStream();
        } catch (Exception e) {
            throw new ServiceException("获取数据失败！请联系相关人员处理");
        }
        return inputStream;
    }
}
